package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.AeProductPostError;
import com.erp.gather.domain.bo.AeProductPostErrorBo;
import com.erp.gather.domain.vo.AeProductPostErrorVo;
import com.erp.gather.mapper.AeProductPostErrorMapper;
import com.erp.gather.service.IAeProductPostErrorService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 商品上传接口错误提示Service业务层处理
 *
 * @author erp
 * @date 2023-04-10
 */
@RequiredArgsConstructor
@Service
public class AeProductPostErrorServiceImpl implements IAeProductPostErrorService {

    private final AeProductPostErrorMapper baseMapper;

    /**
     * 查询商品上传接口错误提示
     */
    @Override
    public AeProductPostErrorVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品上传接口错误提示列表
     */
    @Override
    public TableDataInfo<AeProductPostErrorVo> queryPageList(AeProductPostErrorBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AeProductPostError> lqw = buildQueryWrapper(bo);
        Page<AeProductPostErrorVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品上传接口错误提示列表
     */
    @Override
    public List<AeProductPostErrorVo> queryList(AeProductPostErrorBo bo) {
        LambdaQueryWrapper<AeProductPostError> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AeProductPostError> buildQueryWrapper(AeProductPostErrorBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AeProductPostError> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getErrorCode()), AeProductPostError::getErrorCode, bo.getErrorCode());
        lqw.eq(StringUtils.isNotBlank(bo.getErrorReason()), AeProductPostError::getErrorReason, bo.getErrorReason());
        lqw.eq(StringUtils.isNotBlank(bo.getSolution()), AeProductPostError::getSolution, bo.getSolution());
        return lqw;
    }

    /**
     * 新增商品上传接口错误提示
     */
    @Override
    public Boolean insertByBo(AeProductPostErrorBo bo) {
        AeProductPostError add = BeanUtil.toBean(bo, AeProductPostError.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品上传接口错误提示
     */
    @Override
    public Boolean updateByBo(AeProductPostErrorBo bo) {
        AeProductPostError update = BeanUtil.toBean(bo, AeProductPostError.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AeProductPostError entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品上传接口错误提示
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public AeProductPostError selectByErrorCode(String errorCode) {
        return baseMapper.selectOne(new LambdaQueryWrapper<AeProductPostError>().eq(AeProductPostError::getErrorCode,errorCode));
    }
}
